/*
** Mortem's HGE Addon 1.3
** Copyright (C) 2006-2007, Osokin 'elmortem' Makar
** http://elmortem.mirgames.ru
**
** Windows Vista module.
*/

#include <mhavista.h>

#include <hge.h>
#include <shlwapi.h>
#include <shlobj.h>
#include <stdio.h>

bool mhaGetUserPath(const HGE_CHAR *dir, HGE_CHAR *userpath, int count) {
	HGE *hge = hgeCreate(HGE_VERSION);
	
	HGE_CHAR UserPath[MAX_PATH];
	if(!SUCCEEDED(SHGetSpecialFolderPath(NULL, UserPath, CSIDL_APPDATA, true))) {
    hge->System_Log(TXT("Could not get Application Data."));
    hge->Release();
		return false;
  } else {
		PathAppend(UserPath, dir);
		PathAppend(UserPath, TXT("/"));

		HGE_CHAR p[MAX_PATH];
		int n = HGE_SLEN(UserPath);
		for(int i = 0; i < n; i++) {
			if(UserPath[i] == TXT('\\') || UserPath[i] == TXT('/') || i + 1 == n) {
				HGE_SNCPY(p, UserPath, i + 1);
				p[i + 1] = TXT('\0');
				if(!PathFileExists(p)) {
					if(!CreateDirectory(p, NULL)) { 
						hge->System_Log(TXT("Could not create directory '%s'."), p);
						hge->Release();
						return false;
					} 
				}
			}
		}
	}
	HGE_SNCPY(userpath, UserPath, count);
	
	hge->Release();
	return true;
}